<?php
//error_reporting(E_PARSE);  
session_start();

// SESSION INFO//////////////////////////////////////////////////////////////////////////////////

// UES THE CURRENT SESSION TO DETERMINE QUESTION SHOWN
$currentQ = isset($_SESSION['currentQ']) ? $_SESSION['currentQ'] : 1;
	if (isset($_POST['submitAnswer'])) {
	$currentQ++;}
		else
			{
$currentQ =  1;
			}
			
////WRITE THE SESSION NO TO THE PAGE TO VIEW IT   
$_SESSION['currentQ'] = $currentQ;
		echo "<p class='smiling'>Session: " . $currentQ . "</p>";	
		
		
//STOP THE LEARNER GOING PAST PAGE 10
if 	($currentQ > 10) {
	    echo  "<p class='crying'>End of test";
		header( 'Location: results.php' ) ;

}


///// Get Question from DB ///////////////////////////////////////////////////////////////////////

function getQuestion() {
	
	$db = mysqli_connect ("localhost", "lr12aas", "u3CJFB4r", "dblr12aas");
   
		if (mysqli_connect_errno()) {
			printf ("Connect failed: %s <br />",
			mysqli_connect_error());
			echo "<p class='crying'>Connection problem</p>";
			exit ();
			}
				else {
			echo "<p class='smiling'>Connected</p>";			
		}
	
    $query= "SELECT *"
   	   . " FROM questions ";
     
	 
	$result = mysqli_query($db, $query);

		if (!$result) {
		return false;
			}
	
	//check for rows
	$numberOfRows = mysqli_num_rows($result);
	if ($numberOfRows > 0) {
	$fetchr= array();
	
	//get stuff out of the row
	while ($row = mysqli_fetch_array($result)) {
	$fetchr[] = $row;
		}
	return $fetchr;
		} 		
	}  
   ?>
   
   
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Multiple Choice Assessment</title>
<link href="styles/styles.css" rel="stylesheet" type="text/css">
<script src="scripts/scripts.js" type="text/javascript"></script>
</head>
<body>
<?php
    
//CREATE VARIABLES FOR QUESTION DATA/////////////////////////////////////////////
	  $item = getQuestion();
	  
	  if ($item) {
	  $itemStem = $item[0][0];
	  $option1 = $item[0][1];
	  $option2 = $item[0][2];
	  $option3 = $item[0][3];
	  $option4 = $item[0][4];
	  
	  } 
	  else {
	  echo "<p class='crying'>Could not get item</p>";			
	  }
	
//////CHOSEN ANSWER///////////////////////////////////////////////////////////////////////
 
//SET ALL ITEMS TO ZERO///////////////////////////////////////////////////////////////////////

$option1_status = 0;
$option2_status = 0;
$option3_status = 0;
$option4_status = 0;

$selectedAnswer = $_POST['itemOptions'];

if (isset($_POST['submitAnswer'])) {

//UPDATE TO ONE IF SELECTED ITEM/////////////////////////////////////////////////////////////
if ($selectedAnswer == 'option1') {
	$option1_status = 1;
}

if ($selectedAnswer == 'option2') {
	$option2_status = 1;
}
if ($selectedAnswer == 'option3') {
	$option3_status = 1;
}
if ($selectedAnswer == 'option4') {
	$option4_status = 1;
}
else {
echo "<p class='crying'>Please select an answer</p>";
//STOP LEARNER GOING NEXT AND SESSION INCREMENTING

}
}
	if (isset($_POST['itemOptions'])){
		echo "<p class='smiling'>Selected Answer: " . $selectedAnswer . "</p>";	
		}
		echo "<p class='smiling'>Answer 1 Status: " . $option1_status . "</p>";		
		echo "<p class='smiling'>Answer 2 Status: " . $option2_status . "</p>";	
		echo "<p class='smiling'>Answer 3 Status: " . $option3_status . "</p>";	
		echo "<p class='smiling'>Answer 4 Status: " . $option4_status . "</p>";	
///3 IN A ROW//////////////////////////////////////////////////////////// 		



$source = file_get_contents('test4.php');

$document = new DOMDocument;
$document->loadHTML($source);

//GET CROSS
$element = $document->getElementById('cross'); 

//CHANGE TO TICK
$imageTick = "tick";

echo "<img src='.$imageTick.'.png'>";
?>

		
			<div id="container">
  <header>
    <h1>Multiple Choice Assessment</h1>
  </header>

  <section id="mainContent">
 
    <form id="MC-1" action="" method="POST" class="question">
      <div class="q-stem">
        <p id="itemStem"><?php echo $itemStem ?></p>
      </div>
      <div class="q-option">
        <input name="itemOptions" id="radio1" type="radio" value="option1" class="q-input">
        <label id="option1Lbl" for="radio1" class="form-lbl"> <?php echo $option1 ?></label>
      </div>
      <div class="q-option">
        <input name="itemOptions" id="radio2" type="radio" value="option2" class="q-input">
        <label id="option1Lbl2" for="radio2" class="form-lbl"> <?php echo $option2 ?></label>
      </div>
      <div class="q-option">
        <input name="itemOptions" id="radio3" type="radio" value="option3" class="q-input">
        <label id="option1Lbl3" for="radio3" class="form-lbl"> <?php echo $option3 ?></label>
      </div>
      <div class="q-option">
        <input name="itemOptions" id="radio4" type="radio" value="option4" class="q-input">
        <label id="option1Lbl4" for="radio4" class="form-lbl"> <?php echo $option4 ?></label>
      </div>
      <input name="submitAnswer" id="submitAnswer" type="submit" value="Next" class="q-submit">
    </form>
  </section>
  <footer></footer>
</div>
</body>
</html>


